<template>
  <!-- element-ui级联组件 -->
  <el-cascader
    :value="value"
    separator="-"
    size="mini"
    :options="depts"
    :props="props"
    @change="changValue"
  />
</template>
<script>
import { getDepartment } from '@/api/department'
import { transListToTresData } from '@/utils'
export default {
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      depts: [], // 赋值给 级联组件的options
      props: {
        label: 'name', // 要展示的字段
        value: 'id' // 要存储的字段
      }
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      const result = await getDepartment()
      this.depts = transListToTresData(result, 0) // 将组织架构的数据 转化树形赋值给depts
    },
    changValue(list) {
      console.log(list)
      if (list.length > 0) {
        this.$emit('input', list[list.length - 1])
      } else {
        this.$emit('input', null)
      }
    }
  }
}
</script>

<style scoped lang="scss">
.select-tree{
    width: 300px;
}
</style>
